﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CharList : MonoBehaviour
{
    private static CharList instance;
    public static CharList MyInstance
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType<CharList>();
            }
            return instance;
        }
    }
    bool nameFieldIsActive=false;
    [SerializeField]
    private CharParameters[] parameters;
    [SerializeField]
    Shadow toggleButtonShadow;
    //[SerializeField]
    //private MainStat[] mainStats;
    //[SerializeField]
    //private int abilityPoints;
    [SerializeField]
    private Text APText;
    [SerializeField]
    private InputField nameField;
    //private string playerName;
    [SerializeField]
    private Text alignment;
    //private AbilityPoints ap;
    //public int MyAP { get => abilityPoints;
    //    set
    //    {
    //        abilityPoints = value;
    //        UpdateAPText();
    //        CheckButtonUsability();
    //    }
    //}

    //public string MyPlayerName { get => playerName; set => playerName = value; }

    // Start is called before the first frame update
    void Start()
    {
        
        ResetStats();
        UpdateAPText();
        CheckButtonUsability();
        SetNameActivity(false);
        UpdateAlignmentText();        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void ResetStats()
    {

        UpdateAPText();
        foreach(CharParameters param in parameters)
        {
            param.Lock();
        }
    }
    public void UpdateAPText()
    {
        if (APText != null)
        {

            APText.text = AbilityPoints.MyInstance.MyAP.ToString();
            
        }
        
    }
    public void UseAP(CharParameters charpar)
    {
        if (AbilityPoints.MyInstance.MyAP > 0)
        {
            foreach (CharParameters cp in parameters)
            {
                cp.plusButton.SetActive(true);
            }

            charpar.Click();

            AbilityPoints.MyInstance.MyAP -= 1;
            if (AbilityPoints.MyInstance.MyAP == 0)
            {
                foreach (CharParameters cp in parameters)
                {
                    cp.plusButton.SetActive(false);
                }
            }
        }
        else
        {
            foreach(CharParameters cp in parameters)
            {
                cp.plusButton.SetActive(false);
            }
        }
    }
    public void CheckButtonUsability()
    {
        if (AbilityPoints.MyInstance.MyAP > 0)
        {
            foreach (CharParameters cp in parameters)
            {
                cp.plusButton.SetActive(true);
            }
        }
        else
        {
            foreach (CharParameters cp in parameters)
            {
                cp.plusButton.SetActive(false);
            }
        }
    }
    private void SetNameActivity(bool isActive)
    {
        if (isActive)
        {
            nameField.ActivateInputField();
            nameField.readOnly = false;
        }
        else
        {
            Player.MyInstance.MyName = nameField.text;
            nameField.DeactivateInputField();
            nameField.readOnly=true;
        }
        if (Player.MyInstance.MyName != null)
        {
            nameField.text = Player.MyInstance.MyName;
        }
    }
    public void ToggleButton()
    {
        if (!nameFieldIsActive)
        {
            SetNameActivity(true);
            nameFieldIsActive = true;
            Debug.Log("Active");
            toggleButtonShadow.enabled = true;
        }
        else
        {
            SetNameActivity(false);
            nameFieldIsActive = false;
            Debug.Log("Inactive" + Player.MyInstance.MyName);
            toggleButtonShadow.enabled = false;
        }
    }
    public void UpdateAlignmentText()
    {
        alignment.text = Player.MyInstance.Alignment(Player.MyInstance.MyReputation, Player.MyInstance.MyLawfullness);
    }

}
